﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using Base.Extensions;
using DG.Tweening;
public class LodFBX : MonoBehaviour {
    #region 测试代码部分
    //bool isStartFBX = true;
    //bool isStart = false;
    //int fbxsum;
    //int sum;
    //int fbxallsum;
    //int all;
    //Transform[] fbx;
    //Transform[] trans;
    //List<string> path;
    #endregion
    private Dictionary<string, string> dic;
    private Dictionary<string,Dictionary<string,Base.Data.Message>> msg;
    List<string> path;

    int pathindex;

    public void Start()
    {
        dic = Base.Data.Dictionaries.Fbxmodel;
        msg = Base.Data.Dictionaries.fbxmsgDic;
        path = dic.Keys.ToList();
        transform.localPosition = Vector3.zero;
        ShowThread();
        #region 测试代码部分
        //path = dic.Keys.ToList();
        //fbx = new Transform[path.Count];
        //for (int i = 0; i < path.Count; i++)
        //{
        //    string _path = dic.GetKeyValue<string, string>(path[i]);
        //    GameObject obj = Instantiate(Resources.Load(_path)) as GameObject;
        //    obj.transform.parent = transform;
        //    obj.transform.localPosition = Vector3.zero;
        //    fbx[i] = obj.transform;

        //}
        #endregion

    }
    void Update()
    {
        #region 测试代码部分
        //if (isStartFBX)//加载所有fbx
        //{
        //    if (fbxsum< path.Count)
        //    {
        //        string _path = dic.GetKeyValue<string, string>(path[fbxsum]);
        //        GameObject obj = Instantiate(Resources.Load(_path)) as GameObject;
        //        obj.transform.parent = transform;
        //        obj.transform.localPosition = Vector3.zero;
        //        fbx[fbxsum] = obj.transform;
        //        fbxsum++;
        //        all += fbx[fbxsum].GetComponentsInChildren<Transform>().Length;
        //    }
        //    else
        //    {
        //        fbxsum = 0;
        //        isStartFBX = false;
        //        isStart = true;
        //    }
            
        //}
        //if (isStart)
        //{
        //    if (fbxsum < path.Count)
        //    {
        //        trans= fbx[fbxsum].GetComponentsInChildren<Transform>();
        //        if (sum < trans.Length)
        //        {
        //            if (trans[sum].GetComponent<MeshFilter>() != null)
        //            {
        //                trans[sum].name = path[fbxsum] + "@>" + trans[sum].name;
        //                trans[sum].gameObject.AddComponent<Base.Custom.SingleObject>().SetSingleObject(trans[sum].gameObject);
        //            }
        //            sum++;
        //            fbxallsum++;
        //            print("正在加载" + trans[sum].name+"----------加载进度"+(float)((float)fbxallsum/(float )all)+"%");
        //        }
        //        else
        //        {
        //            //trans[sum-1].gameObject.SetActive(path[fbxsum].Contains("AW"));
        //            fbxsum++;
        //            sum = 0;
        //        }
        //    }
        //    else
        //    {
        //        isStart = false;
        //    }
        //}
        #endregion
    }
    void ShowThread()
    {
        
        foreach (var key in path)
        {
            string _path;
            dic.TryGetValue(key, out _path);
            GameObject obj = Instantiate(Resources.Load(_path)) as GameObject;
            obj.name = key;
            obj.transform.parent = transform;
            obj.transform.localPosition = Vector3.zero;
            Transform[] form = obj.GetComponentsInChildren<Transform>();
            foreach (var item in form)
            {
                if (item.GetComponent<MeshFilter>() != null)
                {
                    //Base.Custom.SingleObject sobj= item.gameObject.AddComponent<Base.Custom.SingleObject>();
                    //item.name = key + "@" + item.name.GetID();
                    item.gameObject.AddComponent<Base.Custom.SingleObject>().SetSingleObject(item.gameObject, obj.name);
                    //sobj.msg = msg.GetKeyValue<string, Base.Data.Message>(item.gameObject.name.GetID());
                }
            }
            Base.Data.Dictionaries.FBXObject.Add(key,obj);

        }
        transform.Center();
        FreeCamera.freecamera.transform.LookAt(transform);
        FreeCamera.freecamera.transform.DOMove(transform.localPosition - FreeCamera.freecamera.transform.localPosition, 0.1f);

    }
    IEnumerator loadFbx()
    {
        while (pathindex< path.Count)
        {
            string _path=dic.GetKeyValue<string,string>(path[pathindex]);
            GameObject obj = Instantiate(Resources.Load(_path)) as GameObject;
            obj.transform.parent = transform;
            obj.transform.localPosition = Vector3.zero;
            Transform[] form = obj.GetComponentsInChildren<Transform>();
            int formindex = 0;
            while (formindex<form.Length)
            {
                if (form[formindex].GetComponent<MeshFilter>()!=null)
                {
                    
                }
            }
            pathindex++;
        }
        yield return 0;
    }
}
